package dk.redia.digiform;

import dk.redia.digiform.utils.NfcScanner;
import dk.redia.digiform.utils.ScanResultListener;
import dk.redia.lib.utils.AppApplication;
import dk.redia.lib.utils.AppConfiguration;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class LaunchFromHomeActivity extends Activity implements ScanResultListener {

	protected static final String TAG = LaunchFromHomeActivity.class.getName();
	TextView textView1;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.launch_from_home);
		
		AppApplication.setApplicationContext(getApplicationContext());
		AppConfiguration.setAppConfiguration(new AppConfiguration() {
			
			private boolean productionMode = true;
			
			@Override
			public void setUiLanguage(String language) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void setContentLanguage(String language) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public String getUiLanguage() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public String getInfoGalleriXmlRpcUrl() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public int getInfoGalleriModelRefreshInterval() {
				// TODO Auto-generated method stub
				return 0;
			}
			
			@Override
			public String getGalleryId() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public String getCustomerId() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public String getContentLanguage() {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public String getApiKey() {
				// TODO Auto-generated method stub
				return null;
			}

			//@Override
			public boolean getProductionMode() {				
				return productionMode;
			}

			//@Override
			public void setProductionMode(boolean productionMode) {
				this.productionMode = productionMode;
				
			}
		});

		Intent intent = getIntent();
		Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
		NfcScanner scanner = new NfcScanner(this);
		scanner.addListener(this);
		scanner.readNfcVTag(tag);
	}

	private void loadResourceData(String resourceId) {
//		pb.setVisibility(View.VISIBLE);
		Intent intent = new Intent(this, ResourceOverviewActivity.class);
		intent.putExtra(ResourceOverviewActivity.EXTRA_RESOURCE_ID, resourceId);
		intent.putExtra("LaunchedFromHomeActivity", true);
		startActivity(intent);	
		overridePendingTransition(R.anim.fadein, R.anim.fadeout);
	}
	
	@Override
	public void scanned(String resourceId) {
		loadResourceData(resourceId);
	}

	@Override
	public void disconnected() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void log(String message) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void error() {
		LayoutInflater inflater = getLayoutInflater();
		View layout = inflater.inflate(R.layout.toast_material_removed_too_fast,
		                               (ViewGroup) findViewById(R.id.toast_material_removed_too_fast));

		Toast toast = new Toast(getApplicationContext());
		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(layout);
		toast.show();
	}
}
